package net.csdn.business.mirror.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.domain.PageDto;
import net.csdn.business.common.domain.common.ConditionQueryDto;
import net.csdn.business.common.domain.mirror.ProjectMirrorDataQueryDto;
import net.csdn.business.common.utils.RedisLockUtils;
import net.csdn.business.common.utils.SurvivalClamProcessor;
import net.csdn.business.mirror.entity.bean.ProjectMirrorData;
import net.csdn.business.mirror.entity.bean.ScheduleTask;
import net.csdn.business.mirror.enums.ProjectMirrorEnum;
import net.csdn.business.mirror.service.ImportMirrorService;
import net.csdn.business.mirror.service.ProjectMirrorDataService;
import net.csdn.business.mirror.service.ScheduleTaskImplement;
import net.csdn.business.mirror.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ScheduleTaskMirrorImplement implements ScheduleTaskImplement {
    @Autowired
    private ProjectMirrorDataService projectMirrorDataService;
    @Autowired
    private ImportMirrorService importMirrorService;

    @Override
    public void exec(ScheduleTask scheduleTask) {
        //Date now = new Date();
        int pageSize = 100;
        int currentCount = 100;
        int pageNum = 1;
        while (currentCount >= pageSize) {
            ProjectMirrorDataQueryDto dto = new ProjectMirrorDataQueryDto();
            dto.setPageNum(pageNum);
            dto.setPageSize(pageSize);
            dto.setOrder(new ConditionQueryDto.Order("next_execution_at", true));
            dto.setStatusStr("-1,1,3,4,6,7"); //根据镜像状态，过滤同步中或导入中的仓库
            dto.setIsSync(ProjectMirrorEnum.SyncStatus.OPEN.code); //查询启动镜像同步开关的镜像仓库配置信息
            dto.setSearchCount(false);
            //dto.setNextSyncExecutionAt(now);
            PageDto<ProjectMirrorData> mirrorDataPaged = projectMirrorDataService.searchData(dto);
            List<ProjectMirrorData> list = mirrorDataPaged.getContent();
            currentCount = list.size();
            if (currentCount < 1) {
                return;
            }
            pageNum += 1;
            importMirrorService.push(list.stream().map(ProjectMirrorData::getId).collect(Collectors.toList()));
        }
    }
}